import SwiftUI
import SPGlassKit

public struct GlassTimelineExample: GlassExample {
    public static let componentID = "glass-timeline"

    // Fictitious delivery log, relative to a fixed instant so that the example is reproducible.
    private static let events: [GlassTimelineEvent] = {
        let start = Date(timeIntervalSince1970: 1_780_300_800)
        func at(_ hours: Double) -> Date { start.addingTimeInterval(hours * 3600) }
        return [
            GlassTimelineEvent(id: "ordered", date: at(0), title: "Order placed", detail: "Order A-1042, three items", systemImage: "cart"),
            GlassTimelineEvent(id: "paid", date: at(0.2), title: "Payment confirmed"),
            GlassTimelineEvent(id: "packed", date: at(21), title: "Parcel packed", detail: "Warehouse 4, aisle 12 — a long note that wraps over several lines on a narrow screen without being cut off."),
            GlassTimelineEvent(id: "shipped", date: at(27.5), title: "Handed to the carrier", systemImage: "shippingbox"),
            GlassTimelineEvent(id: "transit", date: at(52), title: "In transit 🚚", detail: "مركز الفرز · Centre de tri"),
            GlassTimelineEvent(id: "delivered", date: at(76), title: "Delivered", systemImage: "checkmark.circle"),
        ]
    }()

    @State private var order = GlassTimelineOrder.descending
    @State private var showsEmpty = false

    public init() {}

    public var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 16) {
                Picker("Order", selection: $order) {
                    Text("Newest first").tag(GlassTimelineOrder.descending)
                    Text("Oldest first").tag(GlassTimelineOrder.ascending)
                }
                .pickerStyle(.segmented)
                Toggle("Show the empty state", isOn: $showsEmpty)
                GlassTimeline(showsEmpty ? [] : Self.events, order: order, emptyTitle: "Nothing has happened yet")
            }
            .padding()
        }
    }
}

#Preview("GlassTimeline") { GlassTimelineExample() }
#Preview("GlassTimeline — français, Tokyo") {
    GlassTimelineExample()
        .environment(\.locale, Locale(identifier: "fr_CA"))
        .environment(\.timeZone, TimeZone(identifier: "Asia/Tokyo") ?? .gmt)
}
